package model

import (
	"fmt"
	"golang.org/x/crypto/ssh"
	"gopkg.in/yaml.v3"
	"hostname-and-ssh/utils"
	"os"
)

// Host 定义主机结构
type Host struct {
	Name            string `yaml:"name"`
	Address         string `yaml:"address"`
	InternalAddress string `yaml:"internalAddress"`
	User            string `yaml:"user"`
	Password        string `yaml:"password"`
	Client          *ssh.Client
}

// Config 配置文件结构
type Config struct {
	Hosts []Host `yaml:"hosts"`
}

// 读取配置文件
func (c *Config) ReadConfig(path string) error {
	data, err := os.ReadFile(path)
	if err != nil {
		utils.LogError(fmt.Errorf("failed to read config file '%s': %w", path, err))
	}

	//将读取的 YAML 内容解析到 Config 结构体
	err = yaml.Unmarshal(data, c)
	if err != nil {
		utils.LogError(fmt.Errorf("failed to unmarshal YAML data from file '%s': %w", path, err))
		return err
	}
	return nil
}
